package com.xjy.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class loginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        PrintWriter out = null;
        response.setContentType("text/html; charset=UTF-8"); //转码
        out = response.getWriter();

        HttpSession session= request.getSession();
        System.out.println(session.getAttribute("loginInfo"));
        //登录成功之后应该有用户session
        if(request.getRequestURI().contains("Login")){
            return true;
        }
        //说明我在提交登录
        if(request.getRequestURI().contains("login")){
            return true;
        }

        if (session.getAttribute("loginInfo") != null) {
            System.out.println("userInfo=====》"+session.getAttribute("loginInfo"));
            return true;
        }

        out.write("<script>");
        out.write("alert('未登录，请先登录');");
        out.write("location.href='toLoginPage';");
        out.write("</script>");
        out.close();

        return false;
    }
}
